import requests
from bs4 import BeautifulSoup
from lxml import etree
class BtcSpider(object):
    def __init__(self):
        self.url='https://www.chainnode.com/forum/61-{}.html'
        self.headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
        }

    #发请求
    def get_response(self,url):
        response=requests.get(url,headers=self.headers)
        response.encoding=response.apparent_encoding
        data=response.text
        return data

    #解析数据
    def parse_data(self,data,rule):
        # html_data=etree.HTML(data)
        # #模糊查询
        # result_list=html_data.xpath('//div[contains(@class,post-item-wrap)]')
        #
        # print(len(result_list))
        # print(result_list)
        # #following-sibling同级关系查找
        # result_headers=html_data.xpath('//head/following-sibling::*[1]')
        # print(result_headers)

        soup=BeautifulSoup(data,'lxml')
        #类选择器用.+类属性值
        title_lsit=soup.select('.post-item__title a')
        texts = soup.select('.post-item__title a span')
        print(len(texts))
        for title in title_lsit:
            print(title.get('href'))
        for text in range(len(texts)):
            print(texts[text].get_text(),text)
    #保存数据
    # def save_data(self,data):
    #     with open('bs48.html','w') as f:
    #         f.write(data)


    def start(self):
        url=self.url.format(1)
        data=self.get_response(url)
        self.parse_data(data,1)
        #self.save_data(data)

BtcSpider().start()
print(1)